<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Model extends CI_Model {
	
	public $count;
	protected $tbl;
	function __construct(){
		parent::__construct();
		//取得所有表
    	$this->tbl = (object)config_item('tbl');
    	
	}
	
	function save($tbl,$data){

		unset($data['atime']);
		unset($data['utime']);
		
		//过滤非表属性
		$query = $this->db->query("desc ".$tbl);
		$arr_all_field = array();//所有属性
		$arr_key_field = array();//主键属性
		foreach ($query->result() as $r){
			$arr_all_field[] = $r->Field;
			if($r->Key == 'PRI'){
				$arr_key_field[] = $r->Field;
			}
		}
		
		$arr_field = array();//所用属性
		$arr_value = array();//所用值
		foreach ($data as $k=>$v) {
			if(in_array($k, $arr_all_field)){
				$arr_field[] = "`".$k."`";
				$arr_value[] = $this->db->escape($v);
			}
		}
		
		//是插件还是更新
		$insert_flag = false;
		
		if(count($arr_key_field)>0){
			foreach ($arr_key_field as $v){
				if(!in_array('`'.$v.'`', $arr_field)){
					$insert_flag = true;
					break;
				}
			}
		}else{
			$insert_flag = true;
		}

		if($insert_flag){
			
			if(in_array('atime', $arr_all_field)){
				$arr_field[] = '`atime`';
				$arr_value[] = time();
			}
			
			if(in_array('utime', $arr_all_field)){
				$arr_field[] = '`utime`';
				$arr_value[] = time();
			}
			
			$sql = "insert into `".$tbl."` (
				".implode($arr_field, ',')."
			) values (
				".implode($arr_value, ',')."
			)";
			
		}else{
			
			if(in_array('utime', $arr_all_field)){
				$arr_field[] = '`utime`';
				$arr_value[] = time();
			}
			
			$arr_set = array();
			foreach ($arr_field as $k=>$v){
				$arr_set[] = $v."=".$arr_value[$k];
			}
			
			$arr_where = array();
			foreach ($arr_key_field as $v){
				$arr_where[] = "`".$v."`=".$this->db->escape($data[$v]);
			}
			$sql = "update `".$tbl."` set ".implode($arr_set, ',')." 
				where ".implode($arr_where, ' and ');

		}
		
		if($this->db->query($sql)){
			if($insert_flag){
				return $this->db->insert_id();
			}else{
				return $data['id'];
			}		
		}
		
		return 0;
		
	}
	
	function in($tbl,$cond){
		$this->db->from($tbl);
		$this->db->where($cond);
		$count = $this->db->count_all_results();
		if($count>0) return true;
		return false;
	}
	
	function row($tbl,$cond){
		$this->db->from($tbl);
		$this->db->where($cond);
		$query = $this->db->get();
		$list = $query->result();
		if(!empty($list)) return $list[0];
		return array();
	}
	
	function get_by_id($tbl,$id){
		$this->db->from($tbl);
		$this->db->where(array('id'=>$id));
		$query = $this->db->get();
		$list = $query->result();
		if(!empty($list)) return $list[0];
		return array();
	}
	
	function insert($tbl,$data){
		//过滤非表属性
		$query = $this->db->query("desc ".$tbl);
		$arr_all_field = array();//所有属性
		foreach ($query->result() as $r){
			$arr_all_field[] = $r->Field;
		}
		$arr_field = array();//所用属性
		$arr_value = array();//所用值
		foreach ($data as $k=>$v) {
			if(in_array($k, $arr_all_field)){
				$arr_field[] = "`".$k."`";
				$arr_value[] = $this->db->escape($v);
			}
		}
		if(in_array('atime', $arr_all_field)){
			$arr_field[] = '`atime`';
			$arr_value[] = time();
		}
		
		if(in_array('utime', $arr_all_field)){
			$arr_field[] = '`utime`';
			$arr_value[] = time();
		}
		
		$sql = "insert into `".$tbl."` (
			".implode($arr_field, ',')."
		) values (
			".implode($arr_value, ',')."
		)";
		
		
		$this->db->query($sql); 
		
		return true;
		
	}
	
	function delete($tbl,$cond){
		return $this->db->delete($tbl,$cond);
	}
	
	function update($tbl,$data,$cond){
		return $this->db->where($cond)->update($tbl,$data);
	}
	
	function count($tbl,$cond = array()){
		$this->db->from($tbl);
		foreach ($cond as $k => $v) {
			if(is_array($v)){
				$this->db->where_in($k,$v);
			}else{
				$this->db->where($k,$v);
			}
		}
		return $this->db->count_all_results();
	}
	
	function get_all($tbl,$cond = array(),$ord='',$rownum = 0,$rt = 'o'){
		$this->count = $this->count($tbl,$cond);
		$this->db->from($tbl)->where($cond);
		if($rownum>0){
			$this->db->limit($rownum,0);
		}
		if(!empty($ord)){
			$this->db->order_by($ord);
		}
		$query = $this->db->get();
		if($rt == 'o'){
			return $query->result();
		}else{
			return $query->result_array();
		}
	}
	
	function get_page($tbl,$col='*',$cond = array(),$link=array(),$from = 0,$pagesize = 10,$ord='',$rt = 'o'){
		$this->count = $this->count($tbl,$cond);
		return mlist($tbl,$col,$cond,$link,$from,$pagesize,$ord,$rt);		
	}
	
	
}